﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using tingchechang.Models;

namespace tingchechang.Controllers
{
    public class LoginController : Controller
    {
        public LoginDBContext _LoginDBContext;

        public LoginController(LoginDBContext LoginDBContext)
        {
            _LoginDBContext = LoginDBContext;
        }
        // GET: LoginController
        public IActionResult Index()
        {
            return View();
        }

        // GET: LoginController/Details/5
        public IActionResult Details(int id)
        {
            return View();
        }

        // GET: LoginController/Create
        public IActionResult Create()
        {
            return View();
        }
        // POST: LoginController/Create
        public IActionResult Login()
        {
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: LoginController/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: LoginController/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }

        // GET: LoginController/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: LoginController/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id, IFormCollection collection)
        {
            try
            {
                return RedirectToAction(nameof(Index));
            }
            catch
            {
                return View();
            }
        }
        public string LoginCheck(string usernumber, string password)
        {
            string result = "";

            Users user = _LoginDBContext.User.SingleOrDefault(u => u.Userid == usernumber);

            if (user == null)
            {
                result = "账号不存在，请注册！";
            }
            else
            {
                if (user.Password == password)
                {
                    result = "登录成功，正在跳转页面！";
                }
                else
                {
                    result = "密码输入错误，请从新输入！";
                }
            }
            return result;
        }

        public string Check(string usernumber, string password0, string password1)
        {
            string result = "";

            Users user = _LoginDBContext.User.SingleOrDefault(u => u.Userid == usernumber);
            if (user != null)
            {
                result = "该用户已注册，请返回登陆！";
            }
            else
            {
                    if (password1 != password0)
                    {
                        result = "两次密码输入不一致！";
                    }
                    else
                    {
                        result = "注册成功，正在跳转登陆页面！";
                    }
            }
            return result;
        }
        public void Add(string usernumber, string password0, string password1)
        {
            var user = new Users()
            {
                Userid = usernumber,
                Password = password0,
            };
            _LoginDBContext.User.Add(user);
            _LoginDBContext.SaveChanges();

        }
        public void Add1(string cid, string pid, DateTime timein,DateTime timeout)
        {
            var carport = new Carport()
            {
                Carid = cid,
                Portid = pid,
                Timein =timein,
                Timeout =timeout,
            };
            _LoginDBContext.Carport.Add(carport);
            _LoginDBContext.SaveChanges();

        }
        public string Change(string usernumber, string password0, string password1)
        {
            string result = "";

            Users user = _LoginDBContext.User.SingleOrDefault(u => u.Userid == usernumber);

            if (user == null)
            {
                result = "账号不存在，请重新确认！";
            }
            else
            {
                if (password0 != password1)
                {
                    result = "两次密码输入不一致！";
                }
                else

                {
                    result = "密码修改成功!正在返回登录....";
                }
            }
            return result;
        }
        public void Update1(string pid,string cid,DateTime timein,DateTime timeout)
        {
            Carport user = _LoginDBContext.Carport.SingleOrDefault(u => u.Portid== pid);

            user.Portid = pid;
            user.Carid = cid;
            user.Timein = timein;
            user.Timeout = timeout;

            _LoginDBContext.Carport.Update(user);
            _LoginDBContext.SaveChanges();

        }
        public void Remove(string usernumber, string password)
        {
            Users user = _LoginDBContext.User.SingleOrDefault(u => u.Userid == usernumber);
            _LoginDBContext.User.Remove(user);
            _LoginDBContext.SaveChanges();

        }
        public void Update(string usernumber, string password)
        {
            Users user = _LoginDBContext.User.SingleOrDefault(u => u.Userid == usernumber);
            user.Password = password;
            _LoginDBContext.User.Update(user);
            _LoginDBContext.SaveChanges();

        }
        public IActionResult ZhuCe()
        {
            return View();
        }
        public IActionResult ChangePassword()
        {
            return View();
        }
        public List<Users> Look(string key)
        {
            return _LoginDBContext.User.Where(u => u.Userid.Contains(key)).ToList();
        }

        public List<Carport> Look1(string key)
        {
            return _LoginDBContext.Carport.Where(u => u.Carid.Contains(key)).ToList();
        }
        public List<Car> Look2(string key)
        {

            return _LoginDBContext.Car.Where(u => u.Carid.Contains(key)).ToList();
        }
        public List<Port> Look3(string key)
        {
            return _LoginDBContext.Port.Where(u => u.Portid.Contains(key)).ToList();
        }

        public List<Users> Show()
        {
            return _LoginDBContext.User.ToList();
        }
        public List<Carport> Show1()
        {
            return _LoginDBContext.Carport.ToList();
        }
        public List<Port> Show2()
        {
            return _LoginDBContext.Port.ToList();
        }
        public List<Car> Show3()
        {
            return _LoginDBContext.Car.ToList();
        }

    }
}